const { DataTypes } = require('sequelize');
const { hashSync } = require('bcryptjs');

const sequelize = require('../seq');

const User = sequelize.define('User', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false
  },
  username: {
    type: DataTypes.STRING(20),
    allowNull: false,
    validate: {
      isAlphanumeric: true,
      len: [6, 20]
    }
  },
  password: {
    type: DataTypes.STRING(60),
    allowNull: false,
    set(value) {
      this.setDataValue('password', hashSync(value))
    }
  },
  email: {
    type: DataTypes.STRING(254),
    allowNull: false,
    unique: true,
    validate: {
      isEmail: true
    }
  }
}, {
  underscored: true
});

module.exports = User;